﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace FtpClient
{
    class RSAsign
    {
        /// <summary>  
        /// 将传进来的文件转换成字符串  
        /// </summary>  
        /// <param name="FilePath">待处理的文件路径(本地或服务器)</param>  
        /// <returns></returns>
        public string FileToBinary(string filePath)
        {
            //利用新传来的路径实例化一个FileStream对像  
            System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            //得到对象的大小
            int fileLength = Convert.ToInt32(fs.Length);
            //声明一个byte数组 
            byte[] fileByteArray = new byte[fileLength];
            //声明一个读取二进流的BinaryReader对像
            System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
            for (int i = 0; i < fileLength; i++)
            {
                //将数据读取出来放在数组中 
                br.Read(fileByteArray, 0, fileLength);
            }
            //装数组转换为String字符串
            string strData = Convert.ToBase64String(fileByteArray);
            br.Close();
            fs.Close();
            return strData;
        }


        /// <summary>

        /// 签名

        /// </summary>

        /// <param name="str">需签名的数据</param>

        /// <returns>签名后的值</returns>

        public string Sign(string str, string str_privateKey)
        {

            //根据需要加签时的哈希算法转化成对应的hash字符节

            byte[] bt = Encoding.GetEncoding("utf-8").GetBytes(str);

            var sha256 = new SHA256CryptoServiceProvider();

            byte[] rgbHash = sha256.ComputeHash(bt);



            RSACryptoServiceProvider key = new RSACryptoServiceProvider();

            key.FromXmlString(str_privateKey);

            RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(key);

            formatter.SetHashAlgorithm("SHA256");              //此处是你需要加签的hash算法，需要和上边你计算的hash值的算法一致，不然会报错。

            byte[] inArray = formatter.CreateSignature(rgbHash);

            return Convert.ToBase64String(inArray);

        }



        /// <summary>

        /// 签名验证

        /// </summary>

        /// <param name="str">待验证的字符串</param>

        /// <param name="sign">加签之后的字符串</param>

        /// <returns>签名是否符合</returns>

        public bool SignCheck(string str, string sign, string str_publicKey)
        {

            try
            {

                byte[] bt = Encoding.GetEncoding("utf-8").GetBytes(str);

                var sha256 = new SHA256CryptoServiceProvider();

                byte[] rgbHash = sha256.ComputeHash(bt);



                RSACryptoServiceProvider key = new RSACryptoServiceProvider();

                key.FromXmlString(str_publicKey);

                RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(key);

                deformatter.SetHashAlgorithm("SHA256");

                byte[] rgbSignature = Convert.FromBase64String(sign);

                if (deformatter.VerifySignature(rgbHash, rgbSignature))
                {

                    return true;

                }

                return false;

            }

            catch
            {

                return false;

            }

        }

    }
}
